from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from apps.meiduo_admin.serializers.sku_serializer import SKUSerializer, SKUCategorieSerializer, GoodsSimpleSerializer, \
    GoodsSpecSerialzier
from apps.meiduo_admin.utils import PageNum


class SKUModelViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer

    pagination_class = PageNum


#获取三级分类的信息
class SKUCategoriesView(ListAPIView):

    serializer_class = SKUCategorieSerializer
    # 根据数据存储规律parent_id大于37为三级分类信息，查询条件为parent_id__gt=37
    #queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    #或者
    queryset = GoodsCategory.objects.filter(subs=None)

#获取简单的spu数据（用来添加sku时，展示选项）
class GoodsSimpleView(ListAPIView):
    serializer_class = GoodsSimpleSerializer
    queryset = SPU.objects.all()


# 获取SPU商品规格信息
class GoodsSpecView(ListAPIView):

    serializer_class = GoodsSpecSerialzier
    # 因为我们继承的是ListAPIView，在拓展类中是通过get_queryset获取数据，但是我们现在要获取的是规格信息，所以重写get_queryset
    def get_queryset(self):
        #获取地址里传来的spu的idkey为pk
        pk=self.kwargs['pk']
        #根据spuid获取所有规格
        return SPUSpecification.objects.filter(spu_id=pk)